<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
    <!--加载插件-->
    <script src="/static/component/layui_exts/excel.js"></script>
</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
                        <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-col-sm5">
                                <input type="text" name="name" placeholder="" class="layui-input">
                            </div>
                        </div>   
                        <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">编号</label>
                            <div class="layui-col-sm5">
                                <input type="text" name="bianhao" placeholder="" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" id="div1">
                            <label class="layui-form-label">组别</label>
                            <div class="layui-col-sm5">
                                <select name="group">
                                    <!-- <option value="1">1</option>
                                    <option value="2">2</option> -->
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline">
                            <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                                <i class="layui-icon layui-icon-search"></i>
                                查询
                            </button>
                            <button type="reset" class="pear-btn pear-btn-md">
                                <i class="layui-icon layui-icon-refresh"></i>
                                重置
                            </button>
                        </div>
                    </div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="dataTable" lay-filter="dataTable"></table>
			</div>
            <input type="hidden" id="daochufenzu" value="">
		</div>
		<script type="text/html" id="toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增候选
			</button>
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除候选
		    </button>
            {{#  if({$wjy} ==0 ){ }}
            <button class="pear-btn pear-btn-md" lay-event="qingkongshuju">
		        <i class="layui-icon layui-icon-delete"></i>
		        清空得票
		    </button>
            {{#  } else { }}
            
            {{#  } }}
            <button class="pear-btn pear-btn-md" style="float: right;" lay-event="daochu">
                <i class="layui-icon"></i>
                导出Excel
            </button>
		</script>

        

		<script type="text/html" id="options">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="depiaotongji" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;">得票统计</button>
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;">修改候选</button>
		    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="remove" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;">删除候选</button>
            
            {{#  if(d.shenhe ==1 ){ }}
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;">未审核</button>
            {{#  } else { }}
            
            {{#  if(d.ztdp ==0 ){ }}
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;" onclick='state("{{d.id}}")'>开启得票</button>
            {{#  } else { }}
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;" onclick='state("{{d.id}}")'>暂停得票</button>
            {{#  } }}
            
            {{#  } }}
		</script>
        <script type="text/javascript" src="/static/component/layui_exts/jquery.min.js"></script>
        <script>
            function state(id){
                $.ajax({
                    url:'{$Request.root}/admin.zccandidate/state',
                    data:{id:id},
                    dataType: 'json',
                    type: 'POST',
                    success: function(res) {
                        // console.log(res);
                        window.refresh();
                    }
                })
            }
        </script>
        <script>
        layui.use(['table', 'form', 'jquery', 'excel','common','laydate'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let laydate = layui.laydate;
            let excel = layui.excel;
            let MODULE_PATH = "{$Request.root}/admin.zccandidate/";

            $.ajax({
                url:'{$Request.root}/admin.zccandidate/bianhaofenzu',
                data:{biaoid:{$biaoid}},
                dataType: 'json',
                type: 'POST',
                success: function(res) {
                    // console.log(res);
                    if(res.fenzu == 0){
                        $("#div1").css("display","none");
                    }else{
                        var fenzuname = res.fenzuname.split('/');
                        var htmla = '';
                        htmla = "<option value=''></option>";
                        for (var j = 0; j < fenzuname.length; j++) {
                            htmla += "<option value=" +fenzuname[j] + ">" +fenzuname[j] + "</option>";
                        }
                        $("[name='group']").append(htmla);
                        form.render();
                    }
                }
            })


            let cols = [
                [{
                    type: 'checkbox'
                }, {
                    field: "bianhao",
                    title: "编号",
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    field: "name",
                    title: "名称",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "numpiao",
                    title: "票数",
                    sort:true,
                    edit:true,
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    field: "cat",
                    title: "点击量",
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    title: '操作',
                    toolbar: '#options',
                    unresize: true,
                    width:'30%',
                    align: 'center',
                }]
            ]

            let cols1 = [
                [{
                    type: 'checkbox'
                }, {
                    field: "bianhao",
                    title: "编号",
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    field: "name",
                    title: "名称",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "group",
                    title: "组别",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "numpiao",
                    title: "票数",
                    sort:true,
                    edit:true,
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    field: "cat",
                    title: "点击量",
                    unresize: "true",
                    width:'10%',
                    align: "center"
                }, {
                    title: '操作',
                    toolbar: '#options',
                    unresize: true,
                    width:'30%',
                    align: 'center',
                }]
            ]
            
            

            table.on('edit(dataTable)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                // console.log(obj.value); //得到修改后的值
                // console.log(obj.field); //当前编辑的字段名
                // console.log(obj.data.id); //所在行的所有相关数据  
                $.ajax({
                    url:'{$Request.root}/admin.zccandidate/gaipiao',
                    data:{id:obj.data.id,numpiao:obj.value},
                    dataType: 'json',
                    type: 'POST',
                    success: function(res) {
                        // console.log(res);
                        window.refresh();
                    }
                })
            });

            var ywfenzu = {$ywfenzu};
            if(ywfenzu == 0){
                table.render({
                    elem: '#dataTable',
                    url: MODULE_PATH + 'index/id/'+{$biaoid},
                    page: true,
                    cols: cols,
                    cellMinWidth: 100,
                    skin: 'line',
                    toolbar: '#toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print']
                });




                table.on('toolbar(dataTable)', function(obj) {
                    if (obj.event === 'add') {
                        window.add();
                    } else if (obj.event === 'refresh') {
                        window.refresh();
                    } else if (obj.event === 'batchRemove') {
                        window.batchRemove(obj);
                    } else if (obj.event === 'recycle') {
                        window.recycle(obj);
                    } else if (obj.event === 'qingkongshuju') {
                        window.qingkongshuju(obj);
                    } else if (obj.event === 'daochu') {
                        $.ajax({
                            url: '{$Request.root}/admin.zccandidate/getdata',
                            data:{biaoid:{$biaoid},daochufenzu:'wfenzu'},
                            dataType: 'json',
                            success: function(res) {
                                // 假如返回的 res.data 是需要导出的列表数据
                                // console.log(res.data);// [{name: 'wang', age: 18}, {name: 'layui', age: 3}]
                                // console.log(res.datatitle);
                                // LAY_EXCEL.exportExcel(res.data, res.datatitle.title+'得票统计.xlsx', 'xlsx');

                                if ({$biaoid} === 119) {
                                    // 1. 数组头部新增表头
                                    res.data.unshift({name: '名称',bianhao: '编号', videourl: '视频链接', numpiao: '得票', cat: '点击量', xingming: '姓名', zubie: '组别', create_time: '报名时间'});
                                    // 2. 如果需要调整顺序，请执行梳理函数
                                    var data = excel.filterExportData(res.data, [
                                        'name',
                                        'bianhao',
                                        'videourl',
                                        'numpiao',
                                        'cat',
                                        'xingming',
                                        'zubie',
                                        'create_time',
                                    ]);
                                    // 3. 执行导出函数，系统会弹出弹框
                                    excel.exportExcel({
                                        sheet1: data
                                    }, res.datatitle.title+'得票统计.xlsx', 'xlsx');
                                }else{
                                    // 1. 数组头部新增表头
                                    res.data.unshift({name: '名称',bianhao: '编号', tel: '电话', numpiao: '得票', cat: '点击量', create_time: '添加时间'});
                                    // 2. 如果需要调整顺序，请执行梳理函数
                                    var data = excel.filterExportData(res.data, [
                                        'name',
                                        'bianhao',
                                        'tel',
                                        'numpiao',
                                        'cat',
                                        'create_time',
                                    ]);
                                    // 3. 执行导出函数，系统会弹出弹框
                                    excel.exportExcel({
                                        sheet1: data
                                    }, res.datatitle.title+'得票统计.xlsx', 'xlsx');
                                }
                                
                            }
                        });
                    }
                });
            }else{
                table.render({
                    elem: '#dataTable',
                    url: MODULE_PATH + 'index/id/'+{$biaoid},
                    page: true,
                    cols: cols1,
                    cellMinWidth: 100,
                    skin: 'line',
                    toolbar: '#toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print']
                });





                table.on('toolbar(dataTable)', function(obj) {
                    if (obj.event === 'add') {
                        window.add();
                    } else if (obj.event === 'refresh') {
                        window.refresh();
                    } else if (obj.event === 'batchRemove') {
                        window.batchRemove(obj);
                    } else if (obj.event === 'recycle') {
                        window.recycle(obj);
                    } else if (obj.event === 'qingkongshuju') {
                        window.qingkongshuju(obj);
                    } else if (obj.event === 'daochu') {
                        var daochufenzu = $("[id='daochufenzu']").val();
                        $.ajax({
                            url: '{$Request.root}/admin.zccandidate/getdata',
                            data:{biaoid:{$biaoid},daochufenzu:daochufenzu},
                            dataType: 'json',
                            success: function(res) {
                                // 假如返回的 res.data 是需要导出的列表数据
                                // console.log(res.data);// [{name: 'wang', age: 18}, {name: 'layui', age: 3}]
                                // console.log(res.datatitle);
                                // LAY_EXCEL.exportExcel(res.data, res.datatitle.title+'得票统计.xlsx', 'xlsx');

                                // 1. 数组头部新增表头
                                res.data.unshift({name: '名称',bianhao: '编号', tel: '电话', group: '组别', numpiao: '得票', cat: '点击量', create_time: '添加时间'});
                                // 2. 如果需要调整顺序，请执行梳理函数
                                var data = excel.filterExportData(res.data, [
                                    'name',
                                    'bianhao',
                                    'tel',
                                    'group',
                                    'numpiao',
                                    'cat',
                                    'create_time',
                                ]);
                                // 3. 执行导出函数，系统会弹出弹框
                                excel.exportExcel({
                                    sheet1: data
                                }, res.datatitle.title+'得票统计.xlsx', 'xlsx');
                            }
                        });
                    }
                });
            }
            

            table.on('tool(dataTable)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'depiaotongji') {
                    window.depiaotongji(obj);
                }
            });

            

            form.on('submit(query)', function(data) {
                // console.log(data.field.group);
                $("[id='daochufenzu']").val(data.field.group);
                table.reload('dataTable', {
                    where: data.field,
                    page:{curr: 1}
                })
                
                return false;
            });
            
            //弹出窗设置 自己设置弹出百分比
            function screen() {
                if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.8;
                }
                if (typeof height !== 'number' || height === 0) {
                height = $(window).height() - 20;
                }
                return [width + 'px', height + 'px'];
            }

            window.add = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '新增候选',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'add/biaoid/'+{$biaoid},
                });
            }

            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '修改候选',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'edit/id/'+obj.data['id']
                });
            }

            window.depiaotongji = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '得票统计',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'depiaotongji/id/'+obj.data['id']
                });
            }

            window.recycle = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '回收站',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'recycle',
                    cancel: function () {
                        table.reload('dataTable');
                    }
                });
            }

            window.qingkongshuju = function() {
                layer.confirm('确定要清空吗', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    $.ajax({
                        url:'{$Request.root}/admin.zccandidate/qingkongshuju',
                        data:{id:{$biaoid}},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            // console.log(res);
                            layer.close(index);
                            window.refresh();
                        }
                    })
                });
            }


            window.remove = function(obj) {
                layer.confirm('确定要删除该候选', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'remove',
                        data:{id:obj.data['id']},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                    window.refresh();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除这些候选', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'batchRemove',
                        data:{ids:ids,biaoid:{$biaoid}},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function(param) {
                table.reload('dataTable');
            }
        })
    </script>
	</body>
</html>
